#define _CRT_SECURE_NO_WARNINGS
typedef struct
{
    int* a;
    int front;
    int tail;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* ptr = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    ptr->a = (int*)malloc(sizeof(int) * (k + 1));
    ptr->front = 0;
    ptr->tail = 0;
    ptr->k = k;
    return ptr;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }
    else
    {
        obj->a[obj->tail] = value;
        ++obj->tail;
        obj->tail = obj->tail % (obj->k + 1);
        return true;
    }


}

bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    else
    {
        obj->front++;
        obj->front = obj->front % (obj->k + 1);
        return true;
    }
}

int myCircularQueueFront(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
    {
        return obj->a[obj->front];
    }
}

int myCircularQueueRear(MyCircularQueue* obj)
{
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    else
    {
        int i = (obj->tail + obj->k) % (obj->k + 1);
        return obj->a[i];
    }

}

bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
    // if (obj->tail==obj->front)
    // {
    //     return true;
    // }
    // else
    // {
    //     return false;
    // }
    return obj->tail == obj->front;

}

bool myCircularQueueIsFull(MyCircularQueue* obj)
{
    // if((obj->tail+1)%(obj->k+1)==obj->front)
    // {
    //     return true;
    // }
    // else
    // {
    //     return false;
    // }
    return (obj->tail + 1) % (obj->k + 1) == obj->front;
}

void myCircularQueueFree(MyCircularQueue* obj)
{
    free(obj->a);
    free(obj);
}